package com.aiyige.page.publish.coursesingleprice;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.TextView;

import com.aiyige.R;
import com.aiyige.arouter.config.ARouterConfig;
import com.aiyige.page.publish.coursesingleprice.adapter.CourseSinglePriceAdapter;
import com.aiyige.page.publish.selectcourse.model.Course;
import com.aiyige.utils.ToastX;
import com.alibaba.android.arouter.facade.annotation.Autowired;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.chad.library.adapter.base.BaseQuickAdapter;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

@Route(path = ARouterConfig.CourseSinglePricePage)
public class CourseSinglePricePage extends AppCompatActivity {
    public static final String EXTRA_KEY_COURSE_SINGLE_PRICE_DATA = "com.aiyige.extra.EXTRA_KEY_COURSE_SINGLE_PRICE_DATA";
    public static final int REQUEST_CODE_INPUT_SINGLE_PRICE = 1;
    @BindView(R.id.titleTv)
    TextView titleTv;
    @BindView(R.id.mainRv)
    RecyclerView mainRv;
    @Autowired
    Course[] courses;
    CourseSinglePriceAdapter courseSinglePriceAdapter;
    ArrayList<Course> courseList;
    int currentPosition;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.page_course_single_price);
        ButterKnife.bind(this);
        ARouter.getInstance().inject(this);
        titleTv.setText(R.string.set_single_course_price);
        mainRv.setLayoutManager(new LinearLayoutManager(this));
        courseSinglePriceAdapter = new CourseSinglePriceAdapter();
        courseSinglePriceAdapter.bindToRecyclerView(mainRv);
        courseList = new ArrayList<>();
        if (courses != null) {
            for (Course course : courses) {
                courseList.add(course);
            }
        }
        courseSinglePriceAdapter.setNewData(courseList);
        courseSinglePriceAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                if (courseSinglePriceAdapter.getItem(position).isSinglePriceEditable()) {
                    currentPosition = position;
                    ARouter.getInstance()
                            .build(ARouterConfig.InputSinglePricePage)
                            .withString("price", courseSinglePriceAdapter.getItem(position).getSinglePrice())
                            .navigation(CourseSinglePricePage.this, REQUEST_CODE_INPUT_SINGLE_PRICE);
                } else {
                    ToastX.show(R.string.can_not_edit_single_price);
                }
            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        switch (requestCode) {
            case REQUEST_CODE_INPUT_SINGLE_PRICE:
                if (resultCode == RESULT_OK) {
                    Course course = courseSinglePriceAdapter.getItem(currentPosition);
                    course.setSinglePrice(data.getStringExtra(InputSinglePricePage.EXTRA_KEY_PRICE));
                    courseSinglePriceAdapter.setData(currentPosition, course);
                }
                break;
            default:
                super.onActivityResult(requestCode, resultCode, data);
                break;
        }

    }

    @OnClick({R.id.titleBackBtn})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.titleBackBtn:
                finish();
                break;
        }
    }

    @Override
    public void finish() {
        Intent data = new Intent();
        data.putExtra(EXTRA_KEY_COURSE_SINGLE_PRICE_DATA, courseList);
        setResult(RESULT_OK, data);
        super.finish();
    }
}
